var setting = {
	edit : {
		enable : true,
		showRenameBtn : false,
		showRemoveBtn : showRemoveBtn,
		removeTitle : "删除"
	},
	data : {
		simpleData : {
			enable : true,
			idKey : 'pid',
			pIdKey : 'parentPid',
			rootPId : 0
		},
		key : {
			name : 'ddName'
		}
	},
	callback : {
		beforeDrag : beforeDrag,
		beforeRemove : beforeRemove,
		/* onRemove: onRemove, */
		onClick : onClick
	}
};
function showRemoveBtn(treeId, treeNode) {
	return !treeNode.isFirstNode || treeNode.level != 0;
}

// 获取当前节点和所有子节点的ID
function getChildNodes(treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	var childNodes = zTree.transformToArray(treeNode);
	var nodes = '';
	for (var i = 0; i < childNodes.length; i++) {
		nodes = childNodes[i].pid + ',' + nodes;
	}
	return nodes;
}

function beforeRemove(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	layer.confirm('确定删除？', function(data) {
		var nodes = getChildNodes(treeNode);
		var data = {
			pid : nodes
		};
		$.ajax({
			url : App.contextPath + "/Caitlyn/DataDictionary/deleteDictionary",
			type : "POST",
			data : data,
			success : function(json) {
				zTree.removeNode(treeNode);
				layer.msg('删除成功！', {
					icon : 1
				});
			}
		});
		
	}, function(index) {
		
	});
	return false;
}

function beforeDrag(treeId, treeNodes) {
	return false;
}
function onClick(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	nodes = zTree.getSelectedNodes();
	$("#id").val(nodes[0].id);
	// 给字典名称文本框赋值
	$("#dictionaryName").val(nodes[0].ddName);
	// 父ID
	$("#parentPid").val(nodes[0].parentPid);
	// 当前ID
	$("#pid").val(nodes[0].pid);
	// 给字典英文名称文本框赋值
	$("#dictionaryNameEn").val(nodes[0].ddNameEn);
	// 给字典编码文本框赋值
	$("#dictionaryCode").val(nodes[0].ddCode);

	// 最后将返回编辑父项按钮变为增加子项
	$("#bdDataDictionaryDetailsReturnFatherBtn").css('display', 'none');
	$("#bdDataDictionaryDetailsAddChildrenBtn").css('display', 'inline-block');

}

function readyZtree() {
	$.ajax({
		url : App.contextPath + "/Caitlyn/DataDictionary/selectAllDictionary",
		type : "POST",
		success : function(json) {
			zNodes = json;
			$.fn.zTree.init($("#treeDemo"), setting, zNodes);
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			treeObj.expandAll(true);
		}
	});
}
$().ready(function() {
	/* 预加载Ztree */
	readyZtree();
});
// 清空文本框
function clear() {
	$("#id").val("");
	$("#dictionaryName").val("");
	$("#parentPid").val("");
	$("#pid").val("");
	$("#dictionaryNameEn").val("");
	$("#dictionaryCode").val("");
}
var Id = '';
var DicName = '';
var DicNameEn = '';
var DicCode = '';
/** 增加子项 */
function AddChild() {
	// 首先保存文本框当前数据用于返回编辑父项功能使用！
	Id = $("#id").val();
	DicName = $("#dictionaryName").val();
	DicNameEn = $("#dictionaryNameEn").val();
	DicCode = $("#dictionaryCode").val();

	// 然后清除文本框
	$("#id").val("");
	$("#dictionaryName").val("");
	$("#dictionaryNameEn").val("");
	$("#dictionaryCode").val("");

	// 最后将增加子项按钮变为返回编辑父项
	$("#bdDataDictionaryDetailsReturnFatherBtn").css('display', 'inline-block');
	$("#bdDataDictionaryDetailsAddChildrenBtn").css('display', 'none');
}

/**
 * 返回编辑父项
 */
function ReturnFather() {
	// 给文本框赋值
	$("#id").val(Id);
	$("#dictionaryName").val(DicName);
	$("#dictionaryNameEn").val(DicNameEn);
	$("#dictionaryCode").val(DicCode);

	// 清空临时记录的值
	Id = '';
	DicName = '';
	DicNameEn = '';
	DicCode = '';

	// 最后将返回编辑父项按钮变为增加子项
	$("#bdDataDictionaryDetailsReturnFatherBtn").css('display', 'none');
	$("#bdDataDictionaryDetailsAddChildrenBtn").css('display', 'inline-block');
}
/** 保存 */
function save() {
	$.ajax({
		url : App.contextPath + "/Caitlyn/DataDictionary/insertDictionary",
		type : "POST",
		data : $("#upDateDictionaryForm").serialize(),
		success : function(json) {
			if (json == 1) {
				layer.msg("保存成功！",{
					icon : 1
				});
				clear();
				readyZtree();
				// 最后将返回编辑父项按钮变为增加子项
				$("#bdDataDictionaryDetailsReturnFatherBtn").css('display', 'none');
				$("#bdDataDictionaryDetailsAddChildrenBtn").css('display', 'inline-block');
			}else{
				layer.msg("保存失败！",{
					icon : 2
				});
			}
		}
	});
};
